/*
 * @Author: 彭林
 * @Date: 2021-05-13 19:55:08
 * @LastEditTime: 2021-07-01 15:53:52
 * @LastEditors: 彭林
 * @Description:
 * @FilePath: /src/saas-app/app/service/school.go
 */
package service

import (
	"context"
	"saas-app/app/dao"
	"saas-app/app/model"

	"github.com/gogf/gf/util/gvalid"
)

type SchoolRepository interface {
	CreateSchool(campus_name string, user_id uint, school *model.School) bool
}

type school struct{}

func SchoolService() SchoolRepository {
	return &school{}
}

func (s school) CreateSchool(campus_name string, user_id uint, school *model.School) bool {

	if e := gvalid.CheckStruct(context.TODO(), school, nil); e != nil {
		return true
	}

	schoolInfo, err := dao.School.Data(school).Save()

	if err != nil || schoolInfo == nil {
		panic("创建失败")
	}

	id, _ := schoolInfo.LastInsertId()

	var campus model.Campus
	campus.SchoolId = uint(id)
	campus.CampusName = campus_name
	if re := CampusService().CreateCampus(&campus); re {

		userInfo := UserService().GetUserInfoById(user_id)

		var teacher model.SchoolUser
		teacher.SchoolId = uint(id)
		teacher.CampusId = 0
		teacher.Identity = "school"
		teacher.UserId = user_id
		teacher.Sex = 3
		teacher.TeacherName = userInfo.Nickname
		teacher.Phone = userInfo.Passport
		if r := SchoolUserService().CreateTeacher(&teacher); r {
			return true
		} else {
			panic("创建失败")
		}
	} else {
		panic("创建失败")
	}
}
